﻿using System;

namespace Levfier.Proccessors.Filters
{
    /// <summary>
    /// バンドパスフィルタ（IIR）
    /// </summary>
    [Serializable]
    public class BandStopIIR : IIR
    {
        /// <summary>
        /// フィルタ係数を初期化する
        /// </summary>
        /// <param name="sender">イベント発生源</param>
        protected override void InitFilterCoefficient(object sender)
        {
            //デジタル周波数 -> アナログ周波数に変換
            double analogFreq = (Math.Tan(Math.PI * this.cutOffFrequencyParam.Value / VSTParameterManager.GetInstance().SampleRate) / (2.0d * Math.PI));

            //共通の値を計算
            double tempCommon = (2.0d * Math.PI * analogFreq);
            double tempCommon2 = tempCommon * tempCommon;

            //係数計算
            this.b0 = (tempCommon2 + 1.0d) / (1.0d + (tempCommon / this.qualityFactorParam.Value) + tempCommon2);
            this.b1 = 2.0d * (tempCommon2 - 1.0d) / (1.0d + (tempCommon / this.qualityFactorParam.Value) + tempCommon2);
            this.b2 = this.b0;
            this.a1 = (2.0d * tempCommon2 - 2.0d) / (1.0d + (tempCommon / this.qualityFactorParam.Value) + tempCommon2);
            this.a2 = (1.0d - (tempCommon / this.qualityFactorParam.Value) + tempCommon2) / (1 + (tempCommon / this.qualityFactorParam.Value) + tempCommon2);
        }
    }
}
